package com.goods.controller.business;

import com.goods.business.service.IProductService;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA
 *
 * @Date: 2023/3/6 - 18:03
 * @Author: Hyoung
 * @Description:
 */
@RestController
@RequestMapping("/business/product/")
public class ProductController {
    @Autowired
    private IProductService iProductService;

    //findAllStocks?pageSize=9&pageNum=1
    ///www.localhost:8989/business/product/findProductStocks?pageNum=1&pageSize=6
    // TODO 查询条件没有写 business/product/findAllStocks?pageSize=9&pageNum=1
    @GetMapping("/findProductStocks")
    public ResponseBean findProductStocks(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize") Integer pageSize,
                                          @RequestParam(value = "categorys", required = false) String categorys,
                                          @RequestParam(value = "name", required = false) String name) {
        PageVO<ProductStockVO> pageVO = iProductService.findProductStocks(pageNum, pageSize, categorys, name);
        return ResponseBean.success(pageVO);
    }

    //  http://www.localhost:8989/business/product/findProductList?pageNum=1&pageSize=6&name=wo+&categoryId=&supplier=&status=0&categorys=20,21,32
    @GetMapping("/findProductList")
    public ResponseBean findProductList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize") Integer pageSize,
                                        @RequestParam(value = "status") Integer status,
                                        @RequestParam(value = "name", required = false) String name,
                                        @RequestParam(value = "categorys", required = false) String categorys) {
        PageVO<ProductVO> pageVo = iProductService.findProductList(pageNum, pageSize, categorys, status, name);
        return ResponseBean.success(pageVo);
    }

    @PostMapping("/add")
    public ResponseBean addProduct(@RequestBody ProductVO productVO) {
        iProductService.addProduct(productVO);

        return ResponseBean.success();
    }

    @DeleteMapping("/delete/{id}")
    public ResponseBean deleteProductById(@PathVariable Integer id) {
        iProductService.deleteProductById(id);
        return ResponseBean.success();
    }

    @GetMapping("/edit/{id}")
    public ResponseBean editProduct(@PathVariable Integer id) {
        ProductVO productVO = iProductService.findProductById(id);
        return ResponseBean.success(productVO);
    }


    @GetMapping("/findAllStocks")
    public ResponseBean findAllStocks(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize") Integer pageSize) {
        PageVO<ProductStockVO> pageVO = iProductService.findAllStocks(pageNum, pageSize);
        return ResponseBean.success(pageVO);
    }

    ///business/product/findProducts?pageNum=1&pageSize=6&status=0
    //?pageNum=1&pageSize=6&status=0&categorys=24,30,62&name=%E5%8F%91%E5%A4%A7%E6%B0%B4
    @GetMapping("/findProducts")
    public ResponseBean findProducts(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize") Integer pageSize,
                                     @RequestParam(value = "status") Integer status,
                                     ProductVO productVO) {
        PageVO<ProductVO> pageVO = iProductService.findProducts(pageNum, pageSize, status, productVO);
        return ResponseBean.success(pageVO);
    }

    @PutMapping("/publish/{id}")
    public ResponseBean publishProduct(@PathVariable Integer id) {
        iProductService.publish(id);
        return ResponseBean.success();
    }

    @PutMapping("/back/{id}")
    public ResponseBean backProduct(@PathVariable Integer id) {
        iProductService.back(id);
        return ResponseBean.success();
    }

    @PutMapping("/remove/{id}")
    public ResponseBean removeProduct(@PathVariable Integer id) {
        iProductService.remove(id);

        return ResponseBean.success();

    }
}
